import { useEffect } from "react"
import { useNavigate, useLocation } from "react-router-dom"
const WithAuth = ({ children }) => {
  function NewRouter() {
    const navigate = useNavigate()
    const token = localStorage.getItem("token")
    const { pathname } = useLocation()
    const witheList = ["/log"]
    useEffect(() => {
      if (!token && !witheList.includes(pathname)) {
        navigate("*")
      }
    }, [pathname])
    return children
  }
  return <NewRouter />
}

export default WithAuth
